const dataAccess = require('../dataAccess');
const sysModel = dataAccess["main"].model("sysModel");
const sysModelAttr = dataAccess["main"].model("sysModelAttr");
var mkdirByPath = require("../utils/mkdirByPath")
var writeJsFile = require("../utils/writeJsFile")
var beautify = require('js-beautify/js').js
async function main() {
    try {
        var modelPath = mkdirByPath()
        var exportJson = {}
        var modelList = await sysModel.findAll({});
        for(var i=0;i<modelList.length;i++){
            exportJson[modelList[i].modelCode]={}
            exportJson[modelList[i].modelCode]["modelName"]=modelList[i].modelName
            exportJson[modelList[i].modelCode]["modelAttrMap"]={}
            var modelAttrList = await sysModelAttr.findAll({
                "where": {
                    "modelId": modelList[i].modelId
                },
                "order": ['sortNo'],
            });
            for(var j=0;j<modelAttrList.length;j++){
                exportJson[modelList[i].modelCode]
                exportJson[modelList[i].modelCode]["modelAttrMap"][modelAttrList[j].modelAttrName]={"attrName": modelAttrList[j].modelAttrName,"desc": modelAttrList[j].attrDesc}
            }

        }
        // JSON字符串
        let jsonString = JSON.stringify(exportJson)
        // 调用json-beautify函数对JSON字符串进行美化处理
        let formattedJson = beautify(jsonString);
        writeJsFile(modelPath+"/model.json",formattedJson)
        console.log("model.json已生成")
        console.log("文件所处位置"+modelPath)
    } catch (e) {
        console.log(e);
    } finally {
        console.log("操作完成！！！");
    }
}
main()